import { DateTime } from 'luxon'
import Hash from '@ioc:Adonis/Core/Hash'
import {column, beforeSave, BaseModel, hasMany, HasMany, beforeCreate} from '@ioc:Adonis/Lucid/Orm'
import Order from "App/Models/Order";
import uuid from 'uuid-v4'
export default class Users extends BaseModel {
    public static selfAssignPrimaryKey = true
    public static table = 'tb_users'
    @hasMany(() => Order, {
        foreignKey: 'userId',
    })
    public skus: HasMany<typeof Order>
    @column({ isPrimary: true })
    public id: number
    @column()
    public username: string
    @column()
    public email: string

    @column()
    public password: string

    @column()
    public rememberMeToken?: string

    @column.dateTime({ autoCreate: true })
    public createdAt: DateTime

    @column.dateTime({ autoCreate: true, autoUpdate: true })
    public updatedAt: DateTime

    @beforeSave()
    public static async hashPassword (users: Users) {
      if (users.$dirty.password) {
         users.password = await Hash.make(users.password)
      }
    }
    @beforeCreate()
    public static assignUuid(user: Users) {
      user.id = uuid()
    }
}
